package cn.rongdao.jrkabao.ui.deposit.uicomponent;

import android.content.Context;
import android.support.percent.PercentRelativeLayout;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.util.NumberUtil;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;

/**
 * Created by jack on 2017/7/29
 */

public class DepositUIComponent extends PercentRelativeLayout implements View.OnClickListener {
    public DepositUIComponent(Context context) {
        super(context);
    }

    public DepositUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DepositUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private EditText etMoney;
    private ImageView ivClear;
    private TextView tvAvailableBalance;
    private TextView tvDepositAll;

    private float maxMoney;
    private float balance;
    private float mInputMoney;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        etMoney = (EditText) findViewById(R.id.ld_et_money);
        ivClear = (ImageView) findViewById(R.id.ld_iv_clear);
        tvAvailableBalance = (TextView) findViewById(R.id.ld_tv_available_balance);
        tvDepositAll = (TextView) findViewById(R.id.ld_tv_deposit_all);

        initListener();
    }

    private void initListener() {
        etMoney.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        etMoney.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
        etMoney.addTextChangedListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                String money = s.toString();
                if (!FRString.isEmpty(money)) {
                    ivClear.setVisibility(View.VISIBLE);
                    mInputMoney = Float.valueOf(money);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
                if (mInputMoney > maxMoney) {
                    setMaxMoneyToEdit();
                }
            }
        });
        tvDepositAll.setOnClickListener(this);
        ivClear.setOnClickListener(this);
    }

    public void setMaxMoney(String maxMoney) {
        this.maxMoney = Float.valueOf(maxMoney);
    }

    public void setBalance(String balance) {
        tvAvailableBalance.setText(FRCollection.appendAsString("可用余额", NumberUtil.getTwoDecimalPlaces(balance), "元", ""));
        this.balance = Float.valueOf(balance);
    }

    private void setMaxMoneyToEdit() {
        FRToast.showToastSafe(FRCollection.appendAsString("单笔提现金额不能大于", NumberUtil.getTwoDecimalPlaces(maxMoney), "元", ""));
        etMoney.setText(NumberUtil.getTwoDecimalPlaces(maxMoney));
        if (etMoney.getText().toString().length() >= 0)
            etMoney.setSelection(etMoney.getText().toString().length());
    }

    public Float getMoney() {
        return FRString.isEmpty(FRString.getEditTextContent(etMoney)) ? 0 : Float.valueOf(etMoney.getText().toString().trim());
    }

    public EditText getMoneyEditText() {
        return etMoney;
    }

    public boolean isMoneyCorrect() {
        if (FRString.isEmpty(FRString.valueOf(getMoney()))) {
            FRToast.showToastSafe("请输入提现金额");
            return false;
        } else if (getMoney() > balance) {
            FRToast.showToastSafe("您的余额不足");
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ld_iv_clear:
                etMoney.setText("");
                ivClear.setVisibility(View.GONE);
                break;
            case R.id.ld_tv_deposit_all:
                if (balance <= maxMoney) {
                    etMoney.setText(NumberUtil.getTwoDecimalPlaces(balance));
                } else {
                    setMaxMoneyToEdit();
                }
                break;
        }
    }
}
